package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.blade.kit.CollectionKit;
import com.wxxymaker.edu.model.entity.Course;
import com.wxxymaker.edu.model.entity.Note;
import com.wxxymaker.edu.model.entity.Task;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.utils.TimeUtils;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;
import io.github.biezhi.anima.core.Joins;
import io.github.biezhi.anima.page.Page;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author:zhn
 * @Date:2018/10/8
 */
@Bean
@Slf4j
public class NoteService implements Service<Note>{
    @Override
    public Note add(Note note ) {
        note.setTime(TimeUtils.getTime());
        boolean rollback = Anima.atomic(()->{
            Integer asInt=note.save().asInt();
            note.setId(asInt);
        }).catchException(e -> {
            log.error("笔记保存失败！{}",e.getMessage());
        }).isRollback();
        return rollback ? null :note;
    }

    @Override
    public Note delete(Object pk ) {
        return null;
    }

    public boolean delete( Integer id,Integer kid,Integer tid ) {
        boolean rollback = Anima.atomic(()->{
            AnimaQuery<Note> noteAnimaQuery = Anima.delete().from(Note.class);
            if (kid != null)
                noteAnimaQuery.where(Note::getCourseId, kid);
            if (id != null)
                noteAnimaQuery.where(Note::getId, id);
            if (tid != null)
                noteAnimaQuery.where(Note::getTaskId, tid);
            noteAnimaQuery.execute();
        }).catchException(e -> {
            log.error("删除笔记失败!{}",e.getMessage());
        }).isRollback();
        return !rollback;
    }

    @Override
    public boolean change(Note note ) {
        return false;
    }

    public int update(Integer id,Integer flag){
       int  result = Anima.update().from(Note.class).set("flag",flag).where("id",id).execute();
       return result;
    }

    @Override
    public Note byId(Object pk ) {
        return null;
    }

//    public Page<Note> byId(int kid, int tid, Integer page, Integer size) {
//        AnimaQuery<Note> noteAnimaQuery = select().from(Note.class)
//                .where(Note::getCourseId, kid)
//                .where(Note::getTaskId, tid);
//        return noteAnimaQuery.page(page, size);
//    }

    public Note byId(Integer id){
        AnimaQuery<Note> animaQuery = Anima.select().from(Note.class).where(Note::getId, id);
        return animaQuery.one();
    }

    public Page<Note> page(Integer uid, Integer kid, Integer tid,Integer page,Integer size,Integer flag){
        if (uid==null&&kid==null&&tid==null) return null;
        AnimaQuery<Note> query= Anima.select().from(Note.class)
                .join(Joins.with(User.class)
                        .as(Note::getUser)
                        .on(Note::getUserId, User::getId))
                .join(Joins.with(Course.class)
                        .as(Note::getCourse)
                        .on(Note::getCourseId,Course::getId))
                .join(Joins.with(Task.class)
                        .as(Note::getTask)
                        .on(Note::getTaskId, Task::getId));
        if (uid!=null) query.where(Note::getUserId, uid);
        if (kid!=null) query.where(Note::getCourseId, kid);
        if (tid!=null) query.where(Note::getTaskId, tid);
        if (flag!=null)query.where(Note::getFlag,flag);
        Page<Note> page1 = query.page(page, size);
        if (CollectionKit.isNotEmpty(page1.getRows())) {
            return page1;
        }
        return null;
    }

    public long countBykId(Integer courseId) {
        return Anima.select().from(Note.class).where(Note::getCourseId, courseId).count();
    }

    public long countByuId(Integer userId) {
        return Anima.select().from(Note.class).where(Note::getUserId, userId).count();
    }

    public int deleteByKid(Integer kid) {
        return Anima.delete().from(Note.class)
                .where(Note::getCourseId, kid)
                .execute();
    }
}
